package com.sunday.volunteer.ui.product;

import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Cart;
import com.sunday.volunteer.entity.Images;
import com.sunday.volunteer.entity.Product;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.utils.BaseUtil;
import com.sunday.volunteer.utils.StatusBarUtil;
import com.sunday.volunteer.utils.ViewPagerUtils;
import com.sunday.volunteer.weights.GradationScrollView;
import com.sunday.volunteer.window.ProductWindow;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.id.list;

/**
 * Created by admin on 2017/1/10.
 */

public class ProductDetailActivity extends BaseActivity{

    @Bind(R.id.web_view)
    WebView webView;
    @Bind(R.id.viewpager)
    ConvenientBanner viewPager;
    @Bind(R.id.scroll_view)
    GradationScrollView scrollView;
    @Bind(R.id.title_view)
    View title_view;
    @Bind(R.id.title_view_v)
    View title_view_v;
    @Bind(R.id.title_view_v2)
    View title_view_v2;
    @Bind(R.id.exchange)
    TextView exchange;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.price)
    TextView price;
    @Bind(R.id.integral)
    TextView integral;
    @Bind(R.id.old_price)
    TextView old_price;
    @Bind(R.id.count)
    TextView count;
    @Bind(R.id.integral_view)
    View integral_view;
    private int viewHeight ;

    private boolean isShow = true;
    private long id;
    private int windowType = 0;

    //private ShareWindow shareWindow;
    private ProductWindow productWindow;
    private String url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        ButterKnife.bind(this);

        id = getIntent().getLongExtra(BaseUtil.ID , -1);
        url = HttpClient.API_URL+"/volunteer/mobile/product/desc?productId="+id;
        title_view_v.setAlpha(0);
        viewHeight = DeviceUtils.getDisplay(mContext).widthPixels;
        StatusBarUtil.setTransparentForImageView(ProductDetailActivity.this ,title_view);
        viewPager.setLayoutParams(new LinearLayout.LayoutParams(DeviceUtils.getDisplay(mContext).widthPixels , DeviceUtils.getDisplay(mContext).widthPixels));
        webView.loadUrl(url);

        scrollView.setScrollViewListener(new GradationScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) {
                float alpha = (float) (Double.valueOf(y - viewHeight*2/5)/Double.valueOf(viewHeight*2/5));
                float alpha1 = (float) (Double.valueOf(y)/Double.valueOf(viewHeight/2));
                if(y<DeviceUtils.getDisplay(mContext).widthPixels*4/5){
                    StatusBarUtil.setTransparentForImageView(ProductDetailActivity.this ,title_view);
                }else {
                    StatusBarUtil.setColorNoTranslucent(ProductDetailActivity.this , getResources().getColor(R.color.white));
                }
                title_view_v.setAlpha(alpha);
                title_view_v2.setAlpha(1 -alpha1);
            }
        });
        getData();
    }



    //兑换
    @OnClick(R.id.exchange)
    void exchange(){
        if(productWindow == null){
            return;
        }
        productWindow.show(title_view);
    }

    private void getData(){
        Call<ResultDO<Product>> call = HttpClient.getHttpAdapter().findOne(id);
        call.enqueue(new Callback<ResultDO<Product>>() {
            @Override
            public void onResponse(Call<ResultDO<Product>> call, Response<ResultDO<Product>> response) {
                if(response.body() == null || isFinish){
                    return;
                }
                if(response.body().getCode() == 0 && response.body().getResult() != null){
                    Product product = response.body().getResult();
                    //shareWindow = new ShareWindow(mContext , "爱蓝光伏产品" , TextUtils.isEmpty(product.getProduct().getName())?"爱蓝光伏产品":product.getProduct().getName() ,url ,null , null ,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE);
                    //new ViewPagerUtils(mContext ,viewPager ,Arrays.asList(product.getRollImages()));
                    new ViewPagerUtils(mContext ,viewPager ,  product.getRollImages() ,0, true);
                    viewPager.setLayoutParams(new LinearLayout.LayoutParams(DeviceUtils.getDisplay(mContext).widthPixels , DeviceUtils.getDisplay(mContext).widthPixels *2/3));
                    productWindow = new ProductWindow(mContext, product , onClickListener);
                    setData(product);
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Product>> call, Throwable t) {

            }
        });
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                    buyNow();
        }
    };

    private void buyNow(){
        Call<ResultDO<Cart>> call = HttpClient.getHttpAdapter().countBuyNow(BaseApp.getInstance().getMemberId(), id , productWindow.getNum());
        call.enqueue(new Callback<ResultDO<Cart>>() {
            @Override
            public void onResponse(Call<ResultDO<Cart>> call, Response<ResultDO<Cart>> response) {
                if(response.body() == null || isFinish){
                    return;
                }
                if(response.body().getCode() == 0 &&response.body().getResult() != null){
                    productWindow.dismiss();
                    Cart cart = response.body().getResult();
                    BaseUtil.openConfirmOrderActivity(mContext , cart);
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage() );
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Cart>> call, Throwable t) {

            }
        });
    }



    private void setData(Product product){
        title_view_v.setVisibility(View.VISIBLE);
            //积分产品
            name.setText(product.getName());
            integral.setText(product.getPrice()+"");
            old_price.setText(String.format("¥%s" , product.getMarketPrice()+""));
            old_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰
            count.setText(String.format("仅剩%d%s%d%s" , product.getStoreNumber() - product.getSaleNumber() ,"件/共" , product.getStoreNumber() ,"件"));

    }



    public void Share(View view){
//        if(shareWindow == null){
//            shareWindow = new ShareWindow(mContext , "爱蓝光伏产品" ,TextUtils.isEmpty(product.getProduct().getName())?"爱蓝光伏产品":product.getName() ,url ,null , null ,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE);
//        }
//        shareWindow.show(webView);
    }
}
